//
//  LJAnimationListController.m
//  LJAnimationsDemo
//
//  Created by raymond on 17/1/3.
//  Copyright © 2017年 raymond. All rights reserved.
//

#import "LJAnimationListController.h"
#import "LJBiasView.h"
#import "LJAnimationItem.h"
#import "LJItemCell.h"
#import "LJButtonPressAnimationController.h"

static NSString *cellId = @"reuseIndentify";

@interface LJAnimationListController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UILabel *titleNomalLabel;
@property (nonatomic, strong) UILabel *titleAnimalLabel;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, assign) BOOL loadData;

@end

@implementation LJAnimationListController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupNavigationTitle];
    
    [self setupTableView];
    
    [self setupDataSource];
    
    [self setupNotification];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setupNavigationTitle {
    // backgroundView
    LJBiasView *backgroundView = [[LJBiasView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 64)];
    
    // titleView
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 64)];
    
    [titleView addSubview:self.titleNomalLabel];
    [titleView addSubview:self.titleAnimalLabel];
    [titleView addSubview:backgroundView];
    _titleNomalLabel.center = titleView.center;
    _titleAnimalLabel.center = titleView.center;
    UIView *line         = [[UIView alloc] initWithFrame:CGRectMake(0, 63.5, self.view.width, 0.5f)];
    line.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.25f];
    [titleView addSubview:line];
    
    [self.titleView addSubview:titleView];
    
    [self.titleAnimalLabel createGlowLayer];
    [self.titleAnimalLabel insertGlowLayer];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.titleAnimalLabel startGlowLoop];
    });
}

- (void)setupTableView {
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[LJItemCell class] forCellReuseIdentifier:cellId];
    [self.view addSubview:self.tableView];
}

- (void)setupDataSource {
    NSArray *array = @[[LJAnimationItem itemWithName:@"POP-按钮动画" object:[LJButtonPressAnimationController class]],
                       [LJAnimationItem itemWithName:@"POP-Stroke动画" object:nil],
                       [LJAnimationItem itemWithName:@"CAShapeLayer的path动画" object:nil],
                       [LJAnimationItem itemWithName:@"图片碎片化mask动画" object:nil],
                       [LJAnimationItem itemWithName:@"CAGradientLayer动画" object:nil],
                       [LJAnimationItem itemWithName:@"POP-数值动画" object:nil],
                       [LJAnimationItem itemWithName:@"Easing-圆环动画" object:nil],
                       [LJAnimationItem itemWithName:@"UIScrollView视差效果动画" object:nil],
                       [LJAnimationItem itemWithName:@"UIScrollView视差模糊效果" object:nil],
                       [LJAnimationItem itemWithName:@"UITableView状态切换效果" object:nil],
                       [LJAnimationItem itemWithName:@"POP-Spring动画参数详解" object:nil],
                       [LJAnimationItem itemWithName:@"UITableView展开缩放动画" object:nil],
                       [LJAnimationItem itemWithName:@"UITableView显示倒计时" object:nil],
                       [LJAnimationItem itemWithName:@"时钟动画效果" object:nil],
                       [LJAnimationItem itemWithName:@"绘制波形图动画" object:nil],
                       [LJAnimationItem itemWithName:@"UILabel缩放动画" object:nil],
                       [LJAnimationItem itemWithName:@"Facebook辉光动画" object:nil],
                       [LJAnimationItem itemWithName:@"粒子动画-雪花" object:nil],
                       [LJAnimationItem itemWithName:@"刮奖效果" object:nil],
                       [LJAnimationItem itemWithName:@"图片切换效果" object:nil],
                       [LJAnimationItem itemWithName:@"SDWebImage加载图片" object:nil],
                       [LJAnimationItem itemWithName:@"抽象的AlertView" object:nil],
                       [LJAnimationItem itemWithName:@"瀑布流效果" object:nil],
                       [LJAnimationItem itemWithName:@"UILabel混色显示" object:nil],
                       [LJAnimationItem itemWithName:@"翻页效果" object:nil],
                       [LJAnimationItem itemWithName:@"CATransform3D m34" object:nil],
                       [LJAnimationItem itemWithName:@"按钮特效" object:nil],
                       [LJAnimationItem itemWithName:@"心电图动画效果" object:nil],
                       [LJAnimationItem itemWithName:@"音乐波形图动画" object:nil],
                       [LJAnimationItem itemWithName:@"彩色进度条" object:nil],
                       [LJAnimationItem itemWithName:@"果冻效果" object:nil],
                       [LJAnimationItem itemWithName:@"CASpringAnimation" object:nil],
                       [LJAnimationItem itemWithName:@"Additive属性动画" object:nil],
                       [LJAnimationItem itemWithName:@"加载网络数据" object:nil],
                       [LJAnimationItem itemWithName:@"MotionEffect效果" object:nil],
                       [LJAnimationItem itemWithName:@"加载GIF图片" object:nil],
                       [LJAnimationItem itemWithName:@"震动效果" object:nil],
                       [LJAnimationItem itemWithName:@"ScrollView动画" object:nil],
                       [LJAnimationItem itemWithName:@"Cell点击动画" object:nil],
                       [LJAnimationItem itemWithName:@"TextKit简单示例" object:nil],
                       [LJAnimationItem itemWithName:@"线性重复动画" object:nil],
                       [LJAnimationItem itemWithName:@"跑马灯效果" object:nil],
                       [LJAnimationItem itemWithName:@"文本渐变动画效果" object:nil],
                       [LJAnimationItem itemWithName:@"Cell图片视差动画" object:nil],
                       [LJAnimationItem itemWithName:@"系统字体列表" object:nil],
                       [LJAnimationItem itemWithName:@"旋转木马效果" object:nil],
                       [LJAnimationItem itemWithName:@"水平方向瀑布流" object:nil],
                       [LJAnimationItem itemWithName:@"无限轮播图" object:nil],
                       [LJAnimationItem itemWithName:@"BaseControl按钮合集" object:nil],
                       [LJAnimationItem itemWithName:@"POP-缩放" object:nil],
                       [LJAnimationItem itemWithName:@"点击区域的绘制" object:nil],
                       [LJAnimationItem itemWithName:@"QR-Code" object:nil],
                       [LJAnimationItem itemWithName:@"不规则形状的Mask" object:nil],
                       [LJAnimationItem itemWithName:@"水波纹效果" object:nil],
                       [LJAnimationItem itemWithName:@"不规则GridCell" object:nil],
                       [LJAnimationItem itemWithName:@"混合cell" object:nil],];
    
    [self.dataSource addObjectsFromArray:array];
    
    [self.dataSource enumerateObjectsUsingBlock:^(LJAnimationItem *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.index = idx + 1;
    }];
}

- (void)setupNotification {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reciveNotification:) name:noti_showHomeTableView object:nil];
}

- (void)reciveNotification:(NSNotification *)noti {
    dispatch_async(dispatch_get_main_queue(), ^{
        NSMutableArray *indexPaths = [NSMutableArray array];
        for (int i = 0; i < self.dataSource.count; i++) {
            
            [indexPaths addObject:[NSIndexPath indexPathForItem:i inSection:0]];
        }
        
        self.loadData = YES;
        [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
    });
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.loadData ? self.dataSource.count : 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LJItemCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
    LJAnimationItem *item = [self.dataSource objectAtIndex:indexPath.row];
    cell.item = item;
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50.f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    LJAnimationItem *item = [self.dataSource objectAtIndex:indexPath.row];
    if (!item.object) {
        return;
    }
    UIViewController *vc = [[[item.object class] alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - getter

- (UILabel *)titleNomalLabel {
    if (!_titleNomalLabel) {
        _titleNomalLabel = [[UILabel alloc] init];
        _titleNomalLabel.font                          = [UIFont fontWithName:@"Avenir-Light" size:28.f];
        _titleNomalLabel.textAlignment                 = NSTextAlignmentCenter;
        _titleNomalLabel.textColor                     = [UIColor colorWithRed:0.329  green:0.329  blue:0.329 alpha:1];
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"Animations"];
        _titleNomalLabel.attributedText                = attributedString;
        [_titleNomalLabel sizeToFit];
    }
    return _titleNomalLabel;
}

- (UILabel *)titleAnimalLabel {
    if (!_titleAnimalLabel) {
        _titleAnimalLabel = [[UILabel alloc] init];
        _titleAnimalLabel.font                          = [UIFont fontWithName:@"Avenir-Light" size:28.f];
        _titleAnimalLabel.textAlignment                 = NSTextAlignmentCenter;
        NSString *showString                        = @"Animations";
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:showString];
        [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor clearColor] range:NSMakeRange(0, showString.length)];
        [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0.203  green:0.598  blue:0.859 alpha:1]
                                 range:NSMakeRange(1, 1)];
        _titleAnimalLabel.attributedText                = attributedString;
        [_titleAnimalLabel sizeToFit];
    }
    return _titleAnimalLabel;
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.width, self.view.height - 64)];
    }
    return _tableView;
}

@end
